﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OrderAggregator.DataStorage;

namespace OrderAggregator.UI
{
    /// <summary>
    /// Organization control
    /// </summary>
    public partial class OrganizationControl : UserControl
    {        
        private DataStorage.Storage.OrganizationRow _organizationRow = null;        

        /// <summary>
        /// Constructor
        /// </summary>
        public OrganizationControl()
        {
            InitializeComponent();

            cmbParent.DisplayMember = "Name";

            cmbParent.SelectedIndexChanged += cmbParent_SelectedIndexChanged;
            eBxAddres.TextChanged += eBxAddres_TextChanged;
            eBxName.TextChanged += eBxName_TextChanged;
        }

        /// <summary>
        /// Init
        /// </summary>
        /// <param name="organizationDataTable"></param>
        public void Init(Storage.OrganizationDataTable organizationDataTable)
        {
            cmbParent.Items.Clear();
            cmbParent.Items.Add("(нет)");
            cmbParent.Items.AddRange(organizationDataTable.Rows.Cast<object>().ToArray());
        }

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        /// <summary>
        /// Organization
        /// </summary>
        public DataStorage.Storage.OrganizationRow Organization
        {            
            get
            {
                return _organizationRow;
            }
            set
            {
                _organizationRow = value;

                cmbParent.Items.Clear();
                cmbParent.Items.Add("(нет)");

                if (_organizationRow != null)
                {
                    cmbParent.Items.AddRange(GetAllowedParents(_organizationRow));

                    cmbParent.Visible = true;

                    if (_organizationRow.OrganizationRowParent != null)
                    {
                        cmbParent.SelectedItem = _organizationRow.OrganizationRowParent;
                    }
                    else
                    {
                        cmbParent.SelectedIndex = 0;
                    }

                    eBxAddres.Visible = true;
                    eBxAddres.Text = _organizationRow.Addres;
                    eBxName.Visible = true;
                    eBxName.Text = _organizationRow.Name;
                }
                else
                {
                    cmbParent.SelectedIndex = 0;
                    eBxAddres.Visible = false;
                    eBxName.Visible = false;
                }
            }
        }
        

        private void cmbParent_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_organizationRow == null)
            {
                return;
            }
            _organizationRow.OrganizationRowParent = cmbParent.SelectedItem as Storage.OrganizationRow;
        }

        private void eBxAddres_TextChanged(object sender, EventArgs e)
        {
            if (_organizationRow == null)
            {
                return;
            }
            _organizationRow.Addres = eBxAddres.Text;
        }

        private void eBxName_TextChanged(object sender, EventArgs e)
        {
            if (_organizationRow == null)
            {
                return;
            }
            _organizationRow.Name = eBxName.Text;
        }               

        private static Storage.OrganizationRow[] GetAllowedParents(Storage.OrganizationRow organizationRow)
        {
            return ((Storage.OrganizationDataTable) organizationRow.Table).Where(org => !IsChild(organizationRow, org)).ToArray();
        }

        private static bool IsChild(Storage.OrganizationRow parentRow, Storage.OrganizationRow child)
        {
            while (child != null)
            {
                if (child == parentRow)
                {
                    return true;
                }

                child = child.OrganizationRowParent;
            }

            return false;
        }
    }
}
